package com.shanghaionstar.carmanage.chargpile;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.onstar.cn.common.exception.OnStarException;
import com.onstar.cn.common.util.CommonUtil;
import com.onstar.cn.common.util.Constants;
import com.onstar.cn.framework.annotation.FindViewById;
import com.onstar.cn.framework.annotation.OnClick;
import com.onstar.cn.framework.mvp.base.core.BaseFragment;
import com.onstar.cn.map.common.PoiItemInfo;
import com.onstar.cn.map.util.ToastUtil;
import com.onstar.cn.ripple.view.RippleCustomBtn;
import com.onstar.cn.weight.ProgressLayout;
import com.onstar.cn.weight.ToastView;
import com.shanghaionstar.R;
import com.shanghaionstar.activity.VehicleLoadingActivity;
import com.shanghaionstar.carmanage.ChargingPileActivity;
import com.shanghaionstar.carmanage.iview.IChargingPileDetailView;
import com.shanghaionstar.carmanage.model.ChargePileStationEntity;
import com.shanghaionstar.carmanage.presenter.ChargingPileDetailPresenter;
import com.shanghaionstar.utils.AlertDialogUtil;
import com.shanghaionstar.weight.TopLayout;

/**
 * User: jun chen
 * Class:　ChargePileStationDetails
 * Date: 2016-06-12  16:13
 * Description:站点详情页面
 */
public class ChargePileStationDetailsFragment extends BaseFragment implements IChargingPileDetailView{

    private ChargingPileActivity parentActivity;
    @FindViewById(R.id.headview)
    private TopLayout headview;

    @FindViewById(R.id.station_name)
    private TextView station_name;
    @FindViewById(R.id.btn_stationType)
    private TextView btn_stationType;
    @FindViewById(R.id.btn_supplier)
    private TextView btn_supplier;
    @FindViewById(R.id.btn_feeType)
    private TextView btn_feeType;
    @FindViewById(R.id.text_distance_content)
    private TextView text_distance_content;
    @FindViewById(R.id.text_charge_pile_address_info)
    private TextView text_charge_pile_address_info;
    @FindViewById(R.id.fast_charge)
    private TextView fast_charge;
    @FindViewById(R.id.trickle_charge)
    private TextView trickle_charge;
    @FindViewById(R.id.free_charge)
    private TextView free_charge;
    @FindViewById(R.id.btn_sendCar)
    private RippleCustomBtn btn_sendCar;
    @FindViewById(R.id.btn_favorite)
    private RippleCustomBtn btn_favorite;
    @FindViewById(R.id.loading_progress)
    private ProgressLayout loading_progress;

    //标记是否收藏过
    private boolean isFavorite = false;

    private ChargingPileDetailPresenter presenter;


    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        parentActivity = (ChargingPileActivity) getActivity();
        btnBackClick(headview);
        presenter = new ChargingPileDetailPresenter(parentActivity.getScheduler());
        presenter.setView(this);
        presenter.initData((ChargePileStationEntity) getArguments().get(Constants.CHARGEPILEINFO));
    }


    @OnClick({R.id.btn_sendCar, R.id.btn_favorite})
    public void OnClick(View view){
        switch (view.getId()){
            case R.id.btn_sendCar://发送到车
                presenter.sendTbtOrOdd();
                break;
            case R.id.btn_favorite://收藏
                if(!isFavorite){
                    presenter.addFavoriteDestination();
                }else{
                    ToastUtil.show(parentActivity,R.string.charge_pile_collected);
                }
                break;
        }
    }

    @Override
    public int getLayoutId() {
        return R.layout.charging_pile_station_details;
    }


    @Override
    public void initView(ChargePileStationEntity chargePileStationEntity) {
        station_name.setText(chargePileStationEntity.getStationName());

        //站点类型
        if(!CommonUtil.isNull(chargePileStationEntity.getStationType())){
            btn_stationType.setVisibility(View.VISIBLE);
            btn_stationType.setText(chargePileStationEntity.getStationType());
        }else{
            btn_stationType.setVisibility(View.GONE);
        }
        //充电桩供应商
        if(!CommonUtil.isNull(chargePileStationEntity.getSupplier())){
            btn_supplier.setVisibility(View.VISIBLE);
            btn_supplier.setText(chargePileStationEntity.getSupplier());
        }else{
            btn_supplier.setVisibility(View.GONE);
        }
        //充电桩计费类型
        if(!CommonUtil.isNull(chargePileStationEntity.getFeeType())){
            btn_feeType.setVisibility(View.VISIBLE);
            btn_feeType.setText(chargePileStationEntity.getFeeType());
        }else{
            btn_feeType.setVisibility(View.GONE);
        }

//        double distance = Double.parseDouble(chargePileStationEntity.getDistance().toString());
//        BigDecimal bigDecimal = new BigDecimal(distance / 1000);
//        java.text.DecimalFormat df = new java.text.DecimalFormat("#.00");
//        double distance1 = bigDecimal.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
//        String distance2 = df.format(distance1);
        //距离
        text_distance_content.setText(chargePileStationEntity.getDistance());
        //地址信息
        text_charge_pile_address_info.setText(chargePileStationEntity.getAddress());

        String quickCharge = chargePileStationEntity.getQuickCharge();//快充数量
        fast_charge.setText(quickCharge + "个");
        String slowCharge = chargePileStationEntity.getSlowCharge();//慢充数量
        trickle_charge.setText(slowCharge + "个");
        String freeCharge = chargePileStationEntity.getStatus();
        if (getString(R.string.poi_not_city_str).equals(freeCharge))
            freeCharge = "--";
        free_charge.setText(freeCharge);
    }

    @Override
    public void showCommandLoading(boolean show) {
        if(show) {
            startActivity(new Intent(parentActivity, VehicleLoadingActivity.class));
        }
    }

    @Override
    public void setLoading(boolean isLoading) {
        loading_progress.setVisibility(isLoading?View.VISIBLE:View.GONE);
    }

    @Override
    public void onResponseError(OnStarException e) {
        isFavorite = false;
        new ToastView(parentActivity).showToast(getString(R.string.nav_save_favorite_err));
    }

    @Override
    public void onReponseSuccess(String object) {
        isFavorite = true;
        new ToastView(parentActivity).showToast(getString(R.string.poi_save_destination_success));
    }


    @Override
    public Activity onActivity() {
        return getActivity();
    }

    /**
     *
     * 发送tbt失败重试toast
     */
    @Override
    public void showAppFrontNavCommandFailToast(final PoiItemInfo poiItemInfo) {
        AlertDialogUtil.showAlertDialog(parentActivity, getString(R.string.dialog_warning), getString(R.string.nav_command_fail_alert), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
               presenter.sendTbtOrOdd(poiItemInfo);
            }
        });
    }

    /**
     * app在后端时候，发送tbt失败提示toast
     * @param poiItemInfo
     */
    @Override
    public void showAppBackNavCommandFailToast(final PoiItemInfo poiItemInfo) {
        ToastView toastView = new ToastView(parentActivity);
        toastView.setToastClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showAppFrontNavCommandFailToast(poiItemInfo);
            }
        });
    }
}